//
//  HistoryExchangeTableViewController.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/8/14.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "HistoryExchangeTableViewController.h"
#import "HistoryExchangeTableViewCell.h"
#import "HYNetHelper.h"
#import <MJRefresh.h>
#import "ExchangeListEntity.h"
#import "TransfersListEntity.h"
#import "ExchangeDetailsViewController.h"
#import "TransferDetailsViewController.h"
#import <UIScrollView+EmptyDataSet.h>

@interface HistoryExchangeTableViewController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, assign) NSInteger page;

@end

@implementation HistoryExchangeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   [self.tableView registerNib:[UINib nibWithNibName:@"HistoryExchangeTableViewCell" bundle:nil] forCellReuseIdentifier:@"HistoryExchangeTableViewCell"];
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    WEAKSELF
    MJRefreshGifHeader *mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [weakSelf requestDataIsHeaderRefresh:YES];
    }];
    [mj_header setTitle:JFMainLocalizedString(@"loading") forState:MJRefreshStateRefreshing];
    [mj_header setTitle:JFMainLocalizedString(@"MJRefreshHeaderIdleText") forState:MJRefreshStateIdle];
    [mj_header setTitle:JFMainLocalizedString(@"MJRefreshHeaderPullingText") forState:MJRefreshStatePulling];
    self.tableView.mj_header = mj_header;
    [self.tableView.mj_header beginRefreshing];
}

- (void)requestDataIsHeaderRefresh:(BOOL)isHeaderRefresh {
    if (isHeaderRefresh) {
        self.page = 1;
    }
    
    WEAKSELF
    [HYNetHelper transactions2:self.type page:self.page limit:20 callback:^(JFResultVo *resultVo) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        
        if (!resultVo.errorNo) {
            weakSelf.page += 1;
            
            NSArray *array;
            if (weakSelf.type == 2) {
                array = [NSArray yy_modelArrayWithClass:[ExchangeListEntity class] json:resultVo.results[@"data"]];
            }else {
                array = [NSArray yy_modelArrayWithClass:[TransfersListEntity class] json:resultVo.results[@"data"]];
            }
            NSInteger total = [resultVo.results[@"total"] integerValue];
            
            if (isHeaderRefresh) {
                weakSelf.dataArray = [NSArray arrayWithArray:array];
            }else {
                weakSelf.dataArray = [weakSelf.dataArray arrayByAddingObjectsFromArray:array];
            }
            [weakSelf.tableView reloadData];
            
            
            if (total <= 20) {
            }else {
                if (weakSelf.dataArray.count < total) {
                    weakSelf.tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
                        [weakSelf requestDataIsHeaderRefresh:NO];
                    }];
                }else{
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                }
            }
        }
    }];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HistoryExchangeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HistoryExchangeTableViewCell" forIndexPath:indexPath];
    if (self.type == 2) {
        cell.exchangeEntity = self.dataArray[indexPath.row];
    }else {
        cell.transfersEntity = self.dataArray[indexPath.row];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 114.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.type == 2) {
        ExchangeDetailsViewController *vc = [[UIStoryboard storyboardWithName:@"User" bundle:nil]instantiateViewControllerWithIdentifier:@"ExchangeDetailsViewController"];
        ExchangeListEntity *exchangeEntity = self.dataArray[indexPath.row];
        vc.transfer_id = exchangeEntity.ID;
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        TransferDetailsViewController *vc = [[UIStoryboard storyboardWithName:@"User" bundle:nil]instantiateViewControllerWithIdentifier:@"TransferDetailsViewController"];
        TransfersListEntity *transfersEntity = self.dataArray[indexPath.row];
        vc.transfersEntity = transfersEntity;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - DZNEmptyDataSetSource Methods
-(UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return  [UIImage imageNamed:@"nodata"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *title = JFMainLocalizedString(@"myProfile.nodata");
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont fontWithName:@"PingFang SC" size: 14],
                                 NSForegroundColorAttributeName: UIColorFromHex(0x999999)
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView
{
    return YES;
}

@end
